<?php
namespace Home\Controller;

class HelpController extends BaseController {

    public function index(){
        $cates = D('HelpsCate')->field('id, title as value')->select();
        if(empty($cates)){
            $cates = [];
        }

        $this->assign('cates', !empty($cates) ? json_encode($cates):'');

        $cateId = I('cate');
//        $cateId = D('HelpsCate')->getFieldByTitle($cate, 'id');

        $page = I('page', 1);

        $filter = [];
        if(!empty($cateId)){
            $filter['cate_id'] = $cateId;
        }

        $posts = D('Helps')->where($filter)->page($page, 10)->order('id desc')->select();
        foreach ($posts as $k=>$v){
            $posts[$k]['info'] = stringToText($v['content'], 30);
            $posts[$k]['time'] = $v['send_date'];

            unset($v['content']);
            unset($v['send_date']);
        }
        $posts = D('HelpsCate')->parseFieldValueByRelateKey($posts, 'cate_id', 'title', 'tag');

        $this->assign('posts', $posts);

        $postsNum = D('Helps')->where($filter)->count();
        $this->assign('maxpage', ceil($postsNum/10));
        $this->display();
    }
}